Python Deep Learning (Python Technologies) by Vishal Gupta

Python Deep Learning (Python Technologies) by Vishal Gupta
Author: Vishal Gupta [Gupta, Vishal]
Language: eng
Format: azw3, epub
Published: 2020-10-02

Removing an entire table using Python

You can drop a table whenever you need to, using the DROP statement. But you need to be very careful while deleting any existing table because the data lost will not be recovered after deleting a table.

import psycopg2

#establishing the connection

conn = psycopg2. connect(

database = "mydb" , user = 'postgres' , password = 'password' , host = '127.0.0.1' , port = '5432'

)


#Setting auto commit false

conn. autocommit = True

#Creating a cursor object using the cursor() method

cursor = conn. cursor()

#Doping EMPLOYEE table if already exists

cursor. execute( "DROP TABLE emp" )

print ( "Table dropped... " )

#Commit your changes in the database

conn. commit()

#Closing the connection

conn. close()